package com.cabinet.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cabinet.web.entity.Seller;

import cn.com.cabinet.service.SellerServiceImpl;
import cn.com.easy.utils.CookieUtils;

@Controller
@RequestMapping("/seller/info")
public class SellerController {
	
	@Autowired
	private SellerServiceImpl sellerServiceImpl;
	
	@RequestMapping("/redirectUpdate")
	public String redirectUpdatePage(HttpServletRequest request,ModelMap modelMap) throws Exception{
		
		String pfCount = CookieUtils.getCookieValue(request, "pfCount");
		Seller s = sellerServiceImpl.getSellerBysAccount(pfCount);
		modelMap.addAttribute("s", s);
		
		return "/businessBackground/backend/modify_pwd";
		
	}
	
	
	/**
	 * 检测用户原始密码是否正确
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/pwd/check")
	@ResponseBody
	public int checkPwd(String sPwd,HttpServletRequest request) throws Exception{
		String pfCount = CookieUtils.getCookieValue(request, "pfCount");
		if(sellerServiceImpl.getSellerBysAccountAndsPwd(pfCount, sPwd)){
			return 1;
		}else{
			return 0;
		}
	}
	/**
	 * 修改商家账号密码
	 * @param sPwd
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/pwd/update")
	@ResponseBody
	public int UpdatePwd(String sPwd,HttpServletRequest request) throws Exception{
		String pfCount = CookieUtils.getCookieValue(request, "pfCount");
		Seller s = sellerServiceImpl.getSellerBysAccount(pfCount);
		s.setsPwd(sPwd);
		sellerServiceImpl.saveSeller(s);
		return 1;
	}

}
